package com.cloud.bc.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.bc.mapper.BcCategorysMapper;
import com.cloud.bc.model.entity.BcCategorys;
import com.cloud.bc.model.enums.RedisKey;
import com.cloud.bc.service.BcCategorysService;
import com.cloud.common.model.enums.RedisKeyEnum;
import com.cloud.redis.config.RedisUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类
 *
 * @author douhaichao code generator
 * @date 2020-12-31 14:23:48
 */
@Service
@AllArgsConstructor
public class BcCategorysServiceImpl extends ServiceImpl<BcCategorysMapper, BcCategorys> implements BcCategorysService {

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcCategorys selectOne(BcCategorys req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcCategorys>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcCategorys> selectAll(BcCategorys req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcCategorys> selectPage(Page page, BcCategorys req) {
        IPage<BcCategorys> list = baseMapper.selectPage(
                page,
                new QueryWrapper<BcCategorys>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcCategorys> selectPageByLike(Page page, BcCategorys req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getId()) {
            queryWrapper.eq("id", req.getId());
        }

        if (null != req.getParentId()) {
            queryWrapper.eq("parent_id", req.getParentId());
        }

        if (!StrUtil.isEmpty(req.getAlias())) {
            queryWrapper.like("alias", req.getAlias());
        }

        if (!StrUtil.isEmpty(req.getName())) {
            queryWrapper.like("name", req.getName());
        }

        if (null != req.getLevel()) {
            queryWrapper.eq("level", req.getLevel());
        }

        if (null != req.getSort()) {
            queryWrapper.eq("sort", req.getSort());
        }

        if (!StrUtil.isEmpty(req.getNote())) {
            queryWrapper.like("note", req.getNote());
        }

        if (!StrUtil.isEmpty(req.getModule())) {
            queryWrapper.like("module", req.getModule());
        }
        IPage<BcCategorys> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcCategorys req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public List<BcCategorys> selectByIds(String[] ids, String alias) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        queryWrapper.eq("alias", alias);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BcCategorys> selectByIds(List<Integer> ids) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public BcCategorys getCategoryById(String type, Integer id) {
        BcCategorys bcCategorys = new BcCategorys();
        if (type.contains("constellation")) {
            if (id == 1) {
                bcCategorys.setId(1);
                bcCategorys.setName("水瓶座");
            } else if (id == 2) {
                bcCategorys.setId(2);
                bcCategorys.setName("双鱼座");
            } else if (id == 3) {
                bcCategorys.setId(3);
                bcCategorys.setName("白羊座");
            } else if (id == 4) {
                bcCategorys.setId(4);
                bcCategorys.setName("金牛座");
            } else if (id == 5) {
                bcCategorys.setId(5);
                bcCategorys.setName("双子座");
            } else if (id == 6) {
                bcCategorys.setId(6);
                bcCategorys.setName("巨蟹座");
            } else if (id == 7) {
                bcCategorys.setId(7);
                bcCategorys.setName("狮子座");
            } else if (id == 8) {
                bcCategorys.setId(8);
                bcCategorys.setName("处女座");
            } else if (id == 9) {
                bcCategorys.setId(9);
                bcCategorys.setName("天秤座");
            } else if (id == 10) {
                bcCategorys.setId(10);
                bcCategorys.setName("天蝎座");
            } else if (id == 11) {
                bcCategorys.setId(11);
                bcCategorys.setName("射手座");
            } else if (id == 12) {
                bcCategorys.setId(12);
                bcCategorys.setName("摩羯座");
            }
        } else if (type.contains("zodiac")) {
            if (id == 1) {
                bcCategorys.setId(1);
                bcCategorys.setName("鼠");
            } else if (id == 2) {
                bcCategorys.setId(2);
                bcCategorys.setName("牛");
            } else if (id == 3) {
                bcCategorys.setId(3);
                bcCategorys.setName("虎");
            } else if (id == 4) {
                bcCategorys.setId(4);
                bcCategorys.setName("兔");
            } else if (id == 5) {
                bcCategorys.setId(5);
                bcCategorys.setName("龙");
            } else if (id == 6) {
                bcCategorys.setId(6);
                bcCategorys.setName("蛇");
            } else if (id == 7) {
                bcCategorys.setId(7);
                bcCategorys.setName("马");
            } else if (id == 8) {
                bcCategorys.setId(8);
                bcCategorys.setName("羊");
            } else if (id == 9) {
                bcCategorys.setId(9);
                bcCategorys.setName("猴");
            } else if (id == 10) {
                bcCategorys.setId(10);
                bcCategorys.setName("鸡");
            } else if (id == 11) {
                bcCategorys.setId(11);
                bcCategorys.setName("狗");
            } else if (id == 12) {
                bcCategorys.setId(12);
                bcCategorys.setName("猪");
            }
        } else if (type.contains("blood")) {
            if (id == 1) {
                bcCategorys.setId(1);
                bcCategorys.setName("O型");
            } else if (id == 2) {
                bcCategorys.setId(2);
                bcCategorys.setName("A型");
            } else if (id == 3) {
                bcCategorys.setId(3);
                bcCategorys.setName("B型");
            } else if (id == 4) {
                bcCategorys.setId(4);
                bcCategorys.setName("AB型");
            } else if (id == 5) {
                bcCategorys.setId(5);
                bcCategorys.setName("稀有型");
            }
        }
        return bcCategorys;
    }

    private final RedisUtils redisUtils;

    @Override
    public BcCategorys selectByCacheId(Integer id) {
        List<BcCategorys> categorysList = getBcCategorysCache();
        BcCategorys bcCategorys = categorysList.stream().filter(t -> t.getId().equals(id)).findFirst().orElse(new BcCategorys());
        return bcCategorys;
    }

    private List<BcCategorys> getBcCategorysCache() {
        String key = RedisKeyEnum.bcSingleListCategoryRedisKey.getTitle();
        Object o = redisUtils.get(key);
        String data = Convert.toStr(o);
        if (StrUtil.isNotEmpty(data)) {
            return JSONUtil.toList(data, BcCategorys.class);
        }
        List<BcCategorys> categorysList = baseMapper.selectList(null);
        String s = JSONUtil.toJsonStr(categorysList);
        redisUtils.set(key, s);
        return categorysList;
    }

    @Override
    public List<BcCategorys> getCategoryList(String alias) {
        List<BcCategorys> categorysList = getBcCategorysCache();
        return categorysList.stream().filter(t -> t.getAlias().equals(alias)).collect(Collectors.toList());
    }
}
